home *** CD-ROM | disk | FTP | other *** search
/ Especial Multimedia / Especial Multimedia.iso / Multimed / Prg / WAVPLUS.ZIP / DPLIBSTR.BA_ / DPLIBSTR.BA
Text File  |  1997-09-14  |  13KB  |  427 lines

  1. 'DPLIBSTR.BAS
  2. '1/16/95
  3. 'Digital PowerTOOLS Library for Strings
  4. 'Copyright ⌐ 1995 by Digital PowerTOOLS
  5.  
  6. 'these functions and subroutines are intended ONLY for use
  7. 'in your application; you are not authorized to distribute
  8. 'this source code
  9.  
  10. Function AmpersandFix (ThisString)
  11. 'doubles each occurence of an ampersand in the string
  12. 'this enables the string to display ampersands (&) correctly in ListBoxes and Labels
  13. 'VB converts single ampersands to underscores in ListBoxes and Labels
  14.  
  15.     Temp$ = ""
  16.     WorkString$ = ThisString
  17.     
  18.     While InStr(WorkString$, "&")
  19.         n% = InStr(WorkString$, "&")
  20.         Temp$ = Temp$ + Left$(WorkString$, n%) + "&"
  21.         WorkString$ = Mid$(WorkString$, n% + 1)
  22.         Wend
  23.     Temp$ = Temp$ + WorkString$
  24.     AmpersandFix = Temp$
  25. End Function
  26.  
  27. Function AmpersandUnFix (x)
  28. 'if you use AmpersandFix to display ListBox strings correctly,
  29. 'you need to use AmpersandUnfix to remove the double ampersands
  30. 'when using ListBox.List(x) to return the correct string value
  31. 'for example, UserSelection=AmpersandUnfix(List1.List(List1.ListIndex))
  32.  
  33.     Dim z As String
  34.     
  35.     If Len(x) < 1 Then
  36.         AmpersandUnFix = ""
  37.         Exit Function
  38.         End If
  39.     
  40.     z = x
  41.     pos% = InStr(z, "&&")
  42.     Do Until pos% = 0
  43.         z = Left$(z, (pos%)) + Right$(z, Len(z) - Len(y) - pos% - 1)
  44.         pos% = InStr(z, "&&")
  45.         Loop
  46.     AmpersandUnFix = z
  47. End Function
  48.  
  49. Function BackSlashAdd (ThePath)
  50. 'adds a backslash (\) to a string, only if the rightmost
  51. 'character is not already a backslash
  52.  
  53.     ThisPath$ = ThePath
  54.     If Right$(ThisPath$, 1) <> "\" Then
  55.         ThisPath$ = ThisPath$ + "\"
  56.         End If
  57.     BackSlashAdd = ThisPath$
  58. End Function
  59.  
  60. Function BackSlashSub (ThePath)
  61. 'removes the end backslash from a string, if the string is
  62. 'more than three characters in length (not root directory)
  63.  
  64.     ThisPath$ = ThePath
  65.     If Right$(ThisPath$, 1) = "\" And Len(ThisPath$) > 3 Then
  66.         ThisPath$ = Left$(ThisPath$, Len(ThisPath$) - 1)
  67.         End If
  68.     BackSlashSub = ThisPath$
  69. End Function
  70.  
  71. Function Compare (FirstOne, SecondOne)
  72. 'performs a case-insensitive comparison of two strings
  73. 'returns -1 (TRUE) if identical, returns 0 (false) otherwise
  74.  
  75.     ThisFirstOne = UCase$(FirstOne)
  76.     ThisSecondOne = UCase$(SecondOne)
  77.     Compare = False
  78.  
  79.     If ThisFirstOne = ThisSecondOne Then
  80.         Compare = True
  81.         End If
  82.  
  83. End Function
  84.  
  85. Function InstrReverse (Incoming, SearchFor)
  86. 'the opposite of Instr function
  87. 'searches from the END of a string for the first occurence
  88. 'of SearchFor in Incoming
  89.  
  90.     If Len(Incoming) = 0 Or Len(SearchFor) = 0 Then
  91.         InstrReverse = 0
  92.         Exit Function
  93.         End If
  94.  
  95.     IncomingRev = Reverse(Incoming)
  96.     SearchForRev = Reverse(SearchFor)
  97.     pos% = InStr(IncomingRev, SearchForRev)
  98.     If pos% <> 0 Then
  99.         pos% = Len(IncomingRev) - pos% + 1
  100.         End If
  101.     InstrReverse = pos%
  102.  
  103. End Function
  104.  
  105. Function IsLower (Incoming)
  106. 'returns -1 (TRUE) if the first character of Incoming is lower case
  107. 'return 0 (FALSE) if not lower case
  108.  
  109.     IsLower = False
  110.     If Len(Incoming) = 0 Then Exit Function
  111.  
  112.     If Left$(Incoming, 1) >= "a" And Left$(Incoming, 1) <= "z" Then
  113.         IsLower = True
  114.         End If
  115.  
  116. End Function
  117.  
  118. Function IsPathValid (FullPath)
  119. 'determines if the path is a valid DOS path string
  120. 'returns -1 (TRUE) if valid, otherwise returns 0 (FALSE)
  121.  
  122.     If Len(FullPath) < 3 GoTo InvalidPath
  123.     If (InStr(FullPath, "*") <> 0) GoTo InvalidPath
  124.     If (InStr(FullPath, "?") <> 0) GoTo InvalidPath
  125.     If (InStr(FullPath, " ") <> 0) GoTo InvalidPath
  126.     If Mid$(FullPath, 2, 1) <> ":" GoTo InvalidPath
  127.     If UCase$(Left$(FullPath, 1)) < "A" Or UCase$(Left$(FullPath, 1)) > "Z" GoTo InvalidPath
  128.     
  129.     If Len(FullPath) > 2 Then
  130.         If Mid$(FullPath, 3, 1) <> "\" Then
  131.             FullPath = Left$(FullPath, 2) + "\" + Right$(FullPath, Len(FullPath) - 2)
  132.             End If
  133.         End If
  134.  
  135.     If Len(FullPath) = 3 Then
  136.         If Right$(DefaultPath$, 2) = ":\" GoTo ValidPath
  137.         End If
  138.     
  139.     If InStr(FullPath, "\\") <> 0 Then GoTo InvalidPath
  140.         
  141.     FullPath = BackSlashAdd(FullPath)
  142.     LegalChar$ = "!#$%&'()-0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`{}~.ⁿΣ÷─╓▄▀"
  143.     BackPos = 3
  144.     ForePos = InStr(4, FullPath, "\")
  145.     Do
  146.         Temp$ = Mid$(FullPath, BackPos + 1, ForePos - BackPos - 1)
  147.         For i = 1 To Len(Temp$)
  148.             If InStr(LegalChar$, UCase$(Mid$(Temp$, i, 1))) = 0 Then GoTo InvalidPath
  149.             Next i
  150.         PeriodPos = InStr(Temp$, ".")
  151.         Length = Len(Temp$)
  152.         If PeriodPos = 0 Then
  153.             If Length > 8 Then GoTo InvalidPath
  154.             Else
  155.             If PeriodPos > 9 Then GoTo InvalidPath
  156.             If Length > PeriodPos + 3 Then GoTo InvalidPath
  157.             If InStr(PeriodPos + 1, Temp$, ".") <> 0 Then GoTo InvalidPath
  158.             End If
  159.         BackPos = ForePos
  160.         ForePos = InStr(BackPos + 1, FullPath, "\")
  161.     Loop Until ForePos = 0
  162.  
  163.     EndChar$ = Mid$(FullPath, Len(FullPath) - 1, 1)
  164.     If EndChar$ = "." And Mid$(FullPath, Len(FullPath) - 2, 1) = "\" GoTo InvalidPath
  165.     
  166. ValidPath:
  167.     IsPathValid = True
  168.     FullPath = BackSlashSub(FullPath)
  169.     Exit Function
  170. InvalidPath:
  171.     IsPathValid = False
  172.     FullPath = BackSlashSub(FullPath)
  173.     Exit Function
  174. End Function
  175.  
  176. Function IsUpper (Incoming)
  177. 'returns -1 (TRUE) if the first character of Incoming is upper case
  178. 'return 0 (FALSE) if not upper case
  179.  
  180.     IsUpper = False
  181.     If Len(Incoming) = 0 Then Exit Function
  182.  
  183.     If Left$(Incoming, 1) >= "A" And Left$(Incoming, 1) <= "Z" Then
  184.         IsUpper = True
  185.         End If
  186.  
  187. End Function
  188.  
  189. Function JustifyLeft (Incoming, PadChar, TotalWidth)
  190. 'left justifies Incoming$ within TotalWidth% characters using PadChar$ as the pad character
  191. 'if Incoming$ is longer than TotalWidth% it is truncated to TotalWidth% characters
  192.  
  193.     If Len(Incoming) = TotalWidth Then
  194.         JustifyLeft = Incoming
  195.         Exit Function
  196.         End If
  197.     
  198.     If Len(Incoming) > TotalWidth Then
  199.         JustifyLeft = Left$(Incoming, TotalWidth)
  200.         Exit Function
  201.         End If
  202.     
  203.     If Len(PadChar) = 0 Then PadChar = " "
  204.     AddAmount% = TotalWidth - Len(Incoming)
  205.     JustifyLeft = Incoming + String(AddAmount%, Left$(PadChar, 1))
  206.  
  207. End Function
  208.  
  209. Function JustifyRight (Incoming, PadChar, TotalWidth)
  210. 'right justifies Incoming$ within TotalWidth% characters using PadChar$ as the pad character
  211. 'if Incoming$ is longer than TotalWidth% it is truncated to TotalWidth% characters
  212.  
  213.     If Len(Incoming) = TotalWidth Then
  214.         JustifyRight = Incoming
  215.         Exit Function
  216.         End If
  217.     
  218.     If Len(Incoming) > TotalWidth Then
  219.         JustifyRight = Left$(Incoming, TotalWidth)
  220.         Exit Function
  221.         End If
  222.     
  223.     If Len(PadChar) = 0 Then PadChar = " "
  224.     AddAmount% = TotalWidth - Len(Incoming)
  225.     JustifyRight = String(AddAmount%, Left$(PadChar, 1)) + Incoming
  226.  
  227. End Function
  228.  
  229. Function PadLeft (Incoming, PadChar, Count)
  230. 'pads a string (on the left side) with Count% copies of PadChar$
  231. 'in most situations, PadChar$ will be a blank space
  232. 'for example, PadLeft("Now is the","X",4) will return "XXXXNow is the")
  233.  
  234.     If Len(PadChar) = 0 Then PadChar = " "
  235.     PadLeft = String$(Count, Left$(PadChar, 1)) + Incoming
  236. End Function
  237.  
  238. Function PadRight (Incoming, PadChar, Count)
  239. 'pads a string (on the right side) with Count% copies of PadChar$
  240. 'in most situations, PadChar$ will be a blank space
  241. 'for example, PadRight("Now is the","X",4) will return "Now is theXXXX")
  242.  
  243.     If Len(PadChar) = 0 Then PadChar = " "
  244.     PadRight = Incoming + String$(Count, Left$(PadChar, 1))
  245. End Function
  246.  
  247. Function PathDots (FullPath, MaxLength)
  248. 'if the length of FullPath is greater than MaxLenth characters,
  249. 'dots are inserted into the middle of Full Path
  250.  
  251. 'works best if MaxLength is greater than 18 characters
  252. '(this allows for filename, drive, and leading backslash
  253.  
  254.     Dim TempString As String
  255.  
  256.     WorkString = FullPath
  257.     WorkString2 = FullPath
  258.     ThisLength = MaxLength
  259.  
  260.     If Len(WorkString) <= ThisLength Then
  261.         PathDots = WorkString
  262.         Exit Function
  263.         End If
  264.  
  265.     pos% = InStr(WorkString2, "\")
  266.     If pos% <> 0 Then
  267.         WorkString2 = Right$(WorkString2, Len(WorkString2) - pos%)
  268.         NextPos% = InStr(WorkString2, "\")
  269.         If NextPos% <> 0 Then pos% = NextPos% + pos%
  270.         End If
  271.     If pos% = 0 Then pos% = 3
  272.     
  273.     ThisLength = ThisLength - pos%
  274.     For i = Len(WorkString) - ThisLength To Len(WorkString)
  275.         If Mid$(WorkString, i, 1) = "\" Then Exit For
  276.         Next i
  277.  
  278.     PathDots = Left$(WorkString, pos%) + "..." + Right$(WorkString, Len(WorkString) - (i - 1))
  279. End Function
  280.  
  281. Function PathDotsRight (FullPath, MaxLength)
  282. 'truncates a path to MaxLength characters with three trailing elipsis points
  283.  
  284.     WorkString = FullPath
  285.     If Len(FullPath) < MaxLength Or MaxLength < 4 Then
  286.         PathDotsRight = FullPath
  287.         Exit Function
  288.         End If
  289.  
  290.     PathDotsRight = Left$(WorkString, MaxLength - 3) + "..."
  291.  
  292. End Function
  293.  
  294. Function replace (x, y, ReplaceString)
  295. 'replaces ALL occurences of y$ within x$ with ReplaceString
  296. 'for example, strip("abcdefabcedf","cde") = "abfabf"
  297.     
  298.     Dim z As String
  299.     
  300.     If Len(x) < 1 Or Len(y) < 1 Then
  301.         replace = ""
  302.         Exit Function
  303.         End If
  304.  
  305.     If Len(ReplaceString) = 0 Then
  306.         replace = x
  307.         Exit Function
  308.         End If
  309.     
  310.     z = x
  311.     pos% = InStr(z, y)
  312.     Do Until pos% = 0
  313.         z = Left$(z, (pos% - 1)) + ReplaceString + Right$(z, Len(z) - Len(y) - pos% + 1)
  314.         pos% = InStr(z, y)
  315.         Loop
  316.     replace = z
  317. End Function
  318.  
  319. Function Reverse (Incoming)
  320. 'Reverses the character sequence of a string
  321.  
  322.     WorkString = ""
  323.     If Len(Incoming) = 0 Then
  324.         Reverse = ""
  325.         Exit Function
  326.         End If
  327.     
  328.     For i = Len(Incoming) To 1 Step -1
  329.         WorkString = WorkString + Mid$(Incoming, i, 1)
  330.         Next i
  331.     Reverse = WorkString
  332. End Function
  333.  
  334. Function SplitLines (TextMsg, MaxCharsPerLine)
  335. 'splits a long string into multiple lines with hard returns
  336.  
  337.     counter% = 0
  338.     NewTextMsg$ = ""
  339.  
  340.     If MaxCharsPerLine < 15 Then
  341.         SplitLines = TextMsg
  342.         Exit Function
  343.         End If
  344.  
  345.     If Len(TextMsg) < MaxCharsPerLine Then
  346.         SplitLines = TextMsg
  347.         Exit Function
  348.         End If
  349.  
  350.     While Len(TextMsg) > MaxCharsPerLine
  351.         If InStr(TextMsg, Chr$(13)) > MaxCharsPerLine Or InStr(TextMsg, Chr$(13)) = 0 Then
  352.             counter% = MaxCharsPerLine
  353.             While Mid$(TextMsg, counter%, 1) <> " " And counter% > 1
  354.                 counter% = counter% - 1
  355.                 Wend
  356.             If counter% = 1 Then counter% = MaxCharsPerLine
  357.             NewTextMsg$ = NewTextMsg$ + Left$(TextMsg, counter%) + nl
  358.             TextMsg = Right$(TextMsg, Len(TextMsg) - counter%)
  359.             Else
  360.             NewTextMsg$ = NewTextMsg$ + Left$(TextMsg, InStr(TextMsg, Chr$(13)) - 1) + nl
  361.             TextMsg = Right$(TextMsg, Len(TextMsg) - (InStr(TextMsg, Chr$(13)) + 1))
  362.             End If
  363.         Wend
  364.     SplitLines = NewTextMsg$ + TextMsg
  365. End Function
  366.  
  367. Function Strip (x, y)
  368. 'strips ALL occurences of y$ within x$
  369. 'for example, strip("abcdefabcedf","cde") = "abfabf"
  370.     
  371.     Dim z As String
  372.     
  373.     If Len(x) < 1 Or Len(y) < 1 Then
  374.         Strip = ""
  375.         Exit Function
  376.         End If
  377.     
  378.     z = x
  379.     pos% = InStr(z, y)
  380.     Do Until pos% = 0
  381.         z = Left$(z, (pos% - 1)) + Right$(z, Len(z) - Len(y) - pos% + 1)
  382.         pos% = InStr(z, y)
  383.         Loop
  384.     Strip = z
  385. End Function
  386.  
  387. Function stuff (Incoming, AddString, Offset)
  388. 'inserts AddString into Incoming at character position Offset
  389. 'if Offset=len(Incoming)+1 then AddString is just added to the end of Incoming
  390.  
  391.     If Offset < 1 Or Offset > Len(Incoming) + 1 Then
  392.         stuff = Incoming
  393.         Exit Function
  394.         End If
  395.  
  396.     Offset = Offset - 1
  397.     LeftSide$ = Left$(Incoming, Offset)
  398.     RightSide$ = Right$(Incoming, Len(Incoming) - Offset)
  399.  
  400.     stuff = LeftSide$ + AddString + RightSide$
  401.  
  402. End Function
  403.  
  404. Sub Swap (x, y)
  405. 'swaps the values of two variables
  406. 'works with numeric variables too
  407.     
  408.     Dim z As Variant
  409.     
  410.     z = x
  411.     x = y
  412.     y = z
  413. End Sub
  414.  
  415. Function TrimAtNull (TheWord)
  416. 'Trims the string at the NULL character
  417. 'useful with most DLL's that change a string's value
  418.  
  419.     pos% = InStr(TheWord, Chr$(0))
  420.     If pos% = 0 Then
  421.         TrimAtNull = TheWord
  422.         Else
  423.         TrimAtNull = Left$(TheWord, pos% - 1)
  424.         End If
  425. End Function
  426.  
  427.